import { getToken, setToken, removeToken, setRoles, getRoles, removeRoles } from './auth'

let apiUrl = "https://api.ecshop.crlang.com";
// let apiUrl = "http://api.wwj.injoytime.com";

if (apiUrl.indexOf("http://") > -1 || apiUrl.indexOf("https://") > -1 || shopUrl.indexOf("http://") > -1) {
	apiUrl = apiUrl + "/v2/";
} else {
	uni.showModal({
		title: "配置错误",
		content: "apiUrl 地址必须带 http/https",
		showCancel: false
	});
}

function request(param) {
  let header = {
    "content-type": "application/json",
    "X-ECAPI-Sign": "",
    "X-ECAPI-UDID": "",
    "X-ECAPI-UserAgent": "Platform/Wechat",
    "X-ECAPI-Ver": "1.1.0"
  };

  // let token = getToken() || "";
	let token = uni.getStorageSync('token')
  if (token) {
    header["X-ECAPI-Authorization"] = token;
  }
	return uni.request({
	  // url: apiUrl+param.url,
	  url: param.url,
	  method: param.method,
	  data: param.data,
	  header: header,
	});
//   return new Promise(function(resolve, reject) {
//     uni.request({
//       url: apiUrl+url,
//       method: method,
//       data: data,
//       header: header,
//       success: function(res) {
//         if (res.data.error_code === 0 && res.statusCode === 200) {
//           resolve(res.data);
//         } else {
//           reject(res);
//         }
//       },
//       fail: function(err) {
//         showToast("网络加载失败", "error", 600);
//       }
//     });
//   });
}

/**
 * 消息提示
 *
 * @author https://darlang.com
 * @param {*} 内容
 * @param {string} [type="none"] 类型
 * @param {number} [duration=900] 多久消失
 */
function showToast(title, type = "none", duration = 900) {
  let image = "",
    icon = "";
  switch (type) {
    case "error":
      image = "/images/icon_error.png";
      break;
    case "success":
      image = "/images/icon_success.png";
      break;
    case "warning":
      image = "/images/icon_warning.png";
      break;
    case "none":
      icon = "none";
      break;
  }
  uni.showToast({
    title: title,
//     icon: icon,
//     image: image,
    duration: duration,
    mask: true,
    success: function(res) {},
    fail: function(res) {},
    complete: function(res) {}
  });
}


/**
 * 登录异常提示
 *
 * @author https://darlang.com
 * @param {*} err 错误信息
 */
function notLogin(err) {
  // 错误代码
  // "OK": 0, // 正常
  // "UNKNOWN_ERROR": 10000, // 内部错误
  // "TOKEN_INVALID": 10001, // Token 无效
  // "TOKEN_EXPIRED": 10002, // Token 过期
  // "SIGN_INVALID": 10003, // Sign 无效
  // "SIGN_EXPIRED": 10004, // Sign 过期
  let errorCode = [10000, 10001, 10002, 10003, 10004];
  if (err.data.error || err.data.error_code.includes(errorCode)) {
    uni.showModal({
      title: "登录异常",
      content: "由于您长时间未有操作，请重新登录",
      cancelText: "返回首页",
      confirmText: "跳转登录",
      // showCancel: true,
      success: function(cif) {
        if (cif.confirm) {
          wx.navigateTo({
            url: "/pages/auth/login/login"
          });
        } else {
          showToast("由于您尚未授权登录，后续将影响购物。");
          wx.switchTab({
            url: "/pages/index/index"
          });
        }
      }
    });
  }
}

/**
 * 未登录检测
 *
 * @author https://darlang.com
 * @returns
 */
function checkLogin(redirect = false) {
  let user = wx.getStorageSync("user");
  let token = wx.getStorageSync("token");
  if (!user || !token) {
    if (redirect) {
      uni.showModal({
        title: "登录提示",
        content: "由于您尚未登录，请先登录。",
        cancelText: "稍后登录",
        confirmText: "跳转登录",
        // showCancel: true,
        success: function(cif) {
          if (cif.confirm) {
            wx.navigateTo({
              url: "/pages/auth/login/login"
            });
          } else {
            wx.switchTab({
              url: "/pages/index/index"
            });
          }
        }
      });
    }

    return false;
  }
  return true;
}


// 模块出口
module.exports = {
  request,
  showToast,
  apiUrl,
  notLogin,
  checkLogin,
};
